package com.example.testanimation;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;


import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import com.example.testanimation.javabean.User;

import java.util.ArrayList;
import java.util.List;

public class ListStudent extends AppCompatActivity {
    private ListView mlistView;
    private com.example.testanimation.MySQLiteOpenHelper1 mySQLiteOpenHelper1;
    private List<String> mStringList;
    private ArrayAdapter<String> mArryAdpter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Compiler EdgeToEdge = null;
        EdgeToEdge.enable();
        setContentView(R.layout.activity_list_student);
        mlistView = findViewById(R.id.lv);
        mySQLiteOpenHelper1 = new com.example.testanimation.MySQLiteOpenHelper1(this);

        // 查询数据库中所有的用户数据
        List<User> users = mySQLiteOpenHelper1.getAllUsers();
        // 初始化用于ListView的数据列表
        mStringList = new ArrayList<>();

        // 遍历users列表，将每个User对象的信息转换为String，并添加到mStringList中
        for (User user : users) {
            String displayName = user.getStudentName();
            mStringList.add(displayName + " " + user.getClassName());
        }
        // 使用ArrayAdapter来显示数据，适配自定义布局
        mArryAdpter = new MyCustomArrayAdapter(this, mStringList);
        mlistView.setAdapter(mArryAdpter); // 确保这里调用了setAdapter方法
        // 设置长按删除监听器
        mlistView.setOnItemLongClickListener(mLongClickListener);
    }
    //点击添加按钮

    public void addFriend(View view)
    {
        Intent intent = new Intent(ListStudent.this, addFriend.class);
        startActivity(intent);

    }



    private class MyCustomArrayAdapter extends ArrayAdapter<String> {
        public MyCustomArrayAdapter(@NonNull Context context, List<String> strings) {
            super(context, R.layout.listviewimg, strings);
        }

        @NonNull
        @Override
        public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
            View view = LayoutInflater.from(getContext()).inflate(R.layout.listviewimg, parent, false);
            TextView textView = view.findViewById(R.id.textView1); // 使用自定义布局中的TextView ID
            ImageView imageView = view.findViewById(R.id.imageView); // 使用自定义布局中的ImageView ID
            String item = getItem(position); // 使用ArrayAdapter的getItem方法来获取数据
            textView.setText(item);
            // imageView.setImageResource(R.drawable.your_image_resource);
            return view;
        }
    }

    private AdapterView.OnItemLongClickListener mLongClickListener = new AdapterView.OnItemLongClickListener() {
        @Override
        public boolean onItemLongClick(AdapterView<?> parent, View view, final int position, long id) {
            // 创建并显示 AlertDialog 来确认删除操作
            AlertDialog.Builder builder = new AlertDialog.Builder(ListStudent.this);
            builder.setMessage("您确定要删除这个条目吗？");
            builder.setPositiveButton("确认", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    // 用户点击确认，从数据源中移除项
                    mStringList.remove(position);
                    // 更新ListView中的数据
                    mArryAdpter.notifyDataSetChanged();
                    // 可选：给用户一个删除成功的提示
                    Toast.makeText(ListStudent.this, "删除成功", Toast.LENGTH_SHORT).show();
                }
            });
            builder.setNegativeButton("取消", null);
            builder.create().show();

            // 返回true表示已经处理了长按事件
            return true;
        }
    };


    public void goto_class(View view) {
        Intent intent = new Intent(ListStudent.this, java.lang.Class.class);
        startActivity(intent);
    }

    public void goto_self(View view) {
        Intent intent = new Intent(ListStudent.this, com.example.testanimation.Self.class);
        startActivity(intent);
    }
}